package cn.edu.ctbu.datajpacrud.service.impl;


import cn.edu.ctbu.datajpacrud.domain.Teacher;
import cn.edu.ctbu.datajpacrud.repository.TeacherRepository;
import cn.edu.ctbu.datajpacrud.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherImplService implements TeacherService {

    @Autowired
    private TeacherRepository teacherRepository;




    public List<Teacher> findAll() {

        return teacherRepository.findAll();

    }

    /**
     * 只分页的查询
     *
     * @return
     */
    public Page<Teacher> findAll(Pageable pageable){

        return teacherRepository.findAll((pageable) );

    }

    /**
     * 分页，带过滤的查询
     */
    public  Page<Teacher> findAll(Example<Teacher> teacher, Pageable pageable){
        return teacherRepository.findAll(teacher,pageable);
    }
    /**
     * 按id进行查询
     *
     * @param id 主键，整数
     * @return
     */
    public Teacher findById(Integer id) {

        return teacherRepository.findById(id).orElse(null);
    }



    /**
     * 按名字进行查询，like
     *
     * @param name
     * @return
     */
    public List<Teacher> findByName(String name) {

        return teacherRepository.findByNameLike(name);
    }

    /**
     * 按名字和性别进行查询
     * @param name
     * @param sex
     * @return
     */
    public List<Teacher> findByNameAndSex(String name, String sex) {

        return teacherRepository.findByNameAndSex(name, sex);
    }


    /**
     * 查询工号最大的用户
     * @return
     */
    public Teacher getTeacherMaxNo(){
        return teacherRepository.getTeacherMaxNo();
    }
    /**
     * 添加新的用户记录
     *
     * @param teacher
     * @return
     */
    public Teacher insert(Teacher teacher) {

        teacherRepository.save(teacher);
        return teacher;
    }

    /**
     * 更新用户信息
     * @param teacher
     * @return
     */
    public Teacher update(Teacher teacher) {

        teacherRepository.save(teacher);
        return teacher;

    }

    /**
     * 删除用户
     * @param teacher
     */
    public void delete(Teacher teacher){
        teacherRepository.delete(teacher);
    }

    public void delete(Integer id){
        Teacher teacher=new Teacher();
        teacher.setId(id);
        teacherRepository.delete(teacher);
    }
    public Teacher getById(Integer id){
        Teacher teacher=teacherRepository.findById(id).orElse(null);
        return teacher;

    }

//    @Override
//    public Page<Teacher> findAll(Pageable pageable) {
//        return null;
//    }
}
